package com.hqj.service;

import com.hqj.dao.HomesDao;
import com.hqj.entity.Homes;
import com.hqj.util.HightChartUtil;
import com.hqj.util.ReturnValueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HomesServiceI implements BaseService<Homes> {

    @Autowired
    private HomesDao hd;

    @Override
    public List<Homes> find(Homes homes) {
        List<Homes> list = hd.find(homes);
        return list;
    }

    @Override
    public Homes get(Homes homes) {
        List<Homes> list = this.find(homes);
        homes = list.size()>0?list.get(0):null;
        return homes;
    }

    @Override
    public Map<String, Object> add(Homes homes) {
        //判断strDate是否有值
        if(homes.getStrDate()!=null){
            try {
                homes.setBuildDate(new SimpleDateFormat("yyyy-MM-dd").parse(homes.getStrDate()));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        int i = hd.add(homes);
        return ReturnValueUtil.aer(i,"新增");
    }

    @Override
    public Map<String, Object> edit(Homes homes) {
        //判断strDate是否有值
        if(homes.getStrDate()!=null){
            try {
                homes.setBuildDate(new SimpleDateFormat("yyyy-MM-dd").parse(homes.getStrDate()));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        int i = hd.edit(homes);
        return ReturnValueUtil.aer(i,"编辑");
    }

    @Override
    public Map<String, Object> remove(int id) {
        int i = hd.remove(id);
        return ReturnValueUtil.aer(i,"删除");
    }


    public String createBasePie(){

        List<Map<String,Object>> ds = hd.findBasePie();

        String text = "所有房源各种建筑结构的占比";
        List<Map<String,Object>> data = new ArrayList<>();
        int a = 1,b=0;
        for(Map<String,Object> d:ds){
            Map<String,Object> m = new HashMap<>();
            double y = (Double.parseDouble(d.get("num")+""))/(long)d.get("con");//Math.round
            //保留2位小数
            y = Math.round(y*10000)*1.0D/100;
            m.put("name",d.get("jzjg").toString());
            m.put("y",y);
            b++;
            if(a==b) {
                m.put("sliced", true);
                m.put("selected", true);
            }
            data.add(m);
        }
        String json = HightChartUtil.createBasePie(text,data);
        return json;
    }

    public String createLineBarColumn(){
        String text = "每一个小区每一种状态的数量";
        String subText = "数据来源:系统房源信息表";
        String yAxis = "房子套数(套)";
        //获取小区数量
        List<String> categories = hd.findAreasGroup();
        //获取状态数量
        List<String> status = hd.findStatusGroup();
        //获取小区状态数量
        List<Map<String,Object>> ms = hd.findAreaGroupStatusAndID();

        List<Map<String,Object>> list = new ArrayList<>();

        //先循环状态
        for(String stuta:status) {
            Map<String, Object> m = new HashMap<>();
            m.put("name",stuta);
            int data[] = new int[0];
            for(String categorie:categories){
                //获取数量
                int con = getStutaXqNamCon(stuta,categorie,ms);
                data = Arrays.copyOf(data,data.length+1);
                data[data.length-1] = con;
            }
            m.put("data",data);
            list.add(m);
        }

        String result = HightChartUtil.createLineBarColumn(text,subText,yAxis,"套",categories,list);
        return result;
    }

    public int getStutaXqNamCon(String stuta,String xqName,List<Map<String,Object>> list){
        int con = 0;
        for(Map<String,Object> m : list){
            String s = m.get("status")+"";
            String xn = m.get("xqName")+"";
            if(s.equals(stuta)&&xn.equals(xqName)){
                con = Integer.parseInt(m.get("con")+"");
                break;
            }
        }

        return con;
    }

}
